package jatosoft.accounting.dao.hibernate;

import jatosoft.accounting.dao.AccountDao;
import jatosoft.accounting.dao.model.Account;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

public class AccountHibarnateDao extends BaseHibernateDao implements AccountDao {

	@SuppressWarnings("unchecked")
	public List<Account> queryAccount(String parentId) {
		Criteria crit = SessionFactoryUtils.getSession(
				hibernateTemplate.getSessionFactory(), true).createCriteria(
				Account.class);
		if (parentId != null) {
			crit.add(Restrictions.eq("parentAccount.accountId", parentId));
		}
		crit.addOrder(Order.asc("accountId"));
		return crit.list();

	}

	public String saveAccount(Account account) {
		return (String) hibernateTemplate.save(account);
	}

	public void delAccount(Account account) {
		hibernateTemplate.delete(account);
	}

	@SuppressWarnings("unchecked")
	public List<Account> queryMajorAccount() {
		Criteria crit = SessionFactoryUtils.getSession(
				hibernateTemplate.getSessionFactory(), true).createCriteria(
				Account.class);
		crit.add(Restrictions.isNull("parentAccount"));
		return 	crit.list();
	}

}
